<template>

  <div id="app">
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 需求1: 根据给的初始数据, 把购物车页面铺设出来 -->
      <!-- 循环渲染的元素tr -->
      <tr v-for="(item,index) in list" :key="item.id">
        <td>{{index+1}}</td>
        <td>{{item.name}}</td>
        <td>{{item.time}}</td>


        <!-- 需求2: 点击对应删除按钮, 删除对应数据 
            1.根据id删除,给删除按钮绑定点击事件,传递id,阻止默认行为
            2.接受到id以后,根据id找索引值
            3.根据索引删除数据-->
        <td>
          <button @click.prevent="del(item.id)">删除</button>
        </td>
      </tr>

      <!-- 需求3: 当数据没有了, 显示一条提示消息 -->
      <tr v-if="list.length==0">
        <td colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 101, name: "奔驰", time: "2020-08-01" },
        { id: 201, name: "宝马", time: "2020-08-02" },
        { id: 131, name: "奥迪", time: "2020-08-03" },
      ],
    };
  },
  methods: {
    del(id){
      // console.log("我要删除");
      const index=this.list.findIndex(item=>item.id===id)
      this.list.splice(index,1)

    }
  }
};
</script>

<style>
#app {
  width: 600px;
  margin: 10px auto;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>
